﻿#region Disclaimer/License Info

/* *********************************************** */

// sBlog.Net

// sBlog.Net is a minimalistic blog engine software.

// Homepage: http://sblogproject.net
// Github: http://github.com/karthik25/sBlog.Net

// This project is licensed under the BSD license.  
// See the License.txt file for more information.

/* *********************************************** */

#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using sBlog.Net.Domain.Entities;
using sBlog.Net.Domain.Interfaces;
using sBlog.Net.Models;

namespace sBlog.Net.FluentExtensions
{
    public static class PostExtensions
    {
        public static IList<PostEntity> GetPostsByStatus(this List<PostEntity> allPosts, int status)
        {
            var postEntities = allPosts;

            switch (status)
            {
                case 1:
                    postEntities = postEntities.Where(p => p.IsPrivate).ToList();
                    break;
                case 2:
                    postEntities = postEntities.Where(p => !p.IsPrivate).ToList();
                    break;
                case 3:
                    postEntities = postEntities.Where(p => p.IsHome).ToList();
                    break;
                case 4:
                    postEntities = postEntities.Where(p => p.IsHomeUser).ToList();
                    break;
                case 5:
                    postEntities = postEntities.Where(p => p.IsCatHome).ToList();
                    break;
                case 6:
                    postEntities = postEntities.Where(p => p.IsFeature).ToList();
                    break;
                case 7:
                    postEntities = postEntities.Where(p => p.ShowReadPage).ToList();
                    break;
            }

            return postEntities;
        }

        public static BlogPostPageViewModel GetBlogPostPageViewModel(this ICollection<PostEntity> posts, int? pageNumber, ISettings settingsRepository, IPost postsRepository, string rootUrl)
        {
            var pgNumber = pageNumber ?? 1;
            var totalItems = GetPageCount(posts.Count, 10);
            var blogPostModel = GetBlogPostPageModel(pgNumber, totalItems);
            blogPostModel.Posts = GetPostList(posts.ToList(), settingsRepository, postsRepository, rootUrl);
            blogPostModel.BlogName = "Mlog";
            blogPostModel.BlogCaption = "Mlog!";
            blogPostModel.CurrentPageNumber = Convert.ToInt32(pageNumber);
            blogPostModel.DisqusEnabled = false;
            blogPostModel.ShortName = "";
            blogPostModel.DisqusDevMode = System.Web.HttpContext.Current.IsDebuggingEnabled;

            return blogPostModel;
        }

        private static BlogPostPageViewModel GetBlogPostPageModel(int currentPage, int totalPages)
        {
            return new BlogPostPageViewModel
            {
                NextPageValid = currentPage != 1 && totalPages > 1,
                NextPageNumber = currentPage - 1,
                PreviousPageValid = currentPage < totalPages && currentPage != totalPages,
                PreviousPageNumber = currentPage + 1
            };
        }

        private static int GetPageCount(int totalItems, int postsPerPage)
        {
            var totalPages = (int)Math.Ceiling((decimal)totalItems / postsPerPage);
            return totalPages;
        }

        private static List<PostModel> GetPostList(List<PostEntity> postList, ISettings settingsRepository, IPost postsRepository, string rUrl)
        {
            var disqusEnabled = false;
            var rootUrl = rUrl.TrimEnd('/');

            var postModeList = new List<PostModel>();
            //int viewCount = 0;
            //int likeCount = 0;
            postList.ForEach(post =>
            {
                var postModel = new PostModel { Post = post, RootUrl = rootUrl, DisqusEnabled = disqusEnabled };
                //Phi Add 17/12/2013                
                //postsRepository.GetViewAndLikeCount(post.PostID, ref viewCount, ref likeCount);
                //postModel.Post.ViewCount = viewCount;
                //postModel.Post.LikeCount = likeCount;
                //End
                postModeList.Add(postModel);
            });

            return postModeList;
        }
    }
}